package com.LeeCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 等价多米诺骨牌对的数量
 */

public class Code1128 {
    public static void main(String[] args) {

    }

    public int numEquivDominoPairs(int[][] dominoes) {
        int ans = 0;
        Map<String, Integer> map = new HashMap<>();
        int a, b;
        String key;
        for (int[] domino : dominoes) {
            a = Math.min(domino[0], domino[1]);
            b = Math.max(domino[0], domino[1]);
            key = Integer.toString(a) + Integer.toString(b);
            ans += map.getOrDefault(key, 0);
            map.put(key, map.getOrDefault(key, 0) + 1);
        }
        return ans;
    }

    public int numEquivDominoPairs1(int[][] dominoes) {
        int ans = 0;
        int[] aux = new int[100];
        int a, b, key;
        for (int[] domino : dominoes) {
            a = Math.min(domino[0], domino[1]);
            b = Math.max(domino[0], domino[1]);
            key = 10 * a + b;
            ans += aux[key];
            aux[key]++;
        }
        return ans;
    }
}
